###########################################
# Replication file for Rio Negro Province #
###########################################


rm(list=ls(all=TRUE))
#setwd("")
library(eiPack)
library(coda)
library(foreign)

data=read.csv(file.path(getwd(),"data","RN_data.csv"))


# Define row and column marginals of the transition matrix. 
GFPV=data$gral_501
GUCR=data$gral_3
GFAP=data$gral_534
GO=data$gral_14
GN=data$votantes-GFPV-GUCR-GFAP-GO


PFPV=data$paso_501
PUCRA=data$paso_3078
PUCRB=data$paso_3079
PUCRC=data$paso_3080
PFAP=data$paso_534
PO=data$paso_3081+data$paso_3082+data$paso_14
PN=data$votantes-PFPV-PUCRA-PUCRB-PUCRC-PFAP-PO

data1=data.frame(PFPV,PUCRA,PUCRB,PUCRC,PFAP,PO,PN,GFPV,GUCR,GFAP,GO,GN)



# Tunning the EI algorithm
tune.nocov=tuneMD(cbind(GFPV,GUCR,GFAP,GO,GN)~cbind(PFPV,PUCRA,PUCRB,PUCRC,PFAP,PO,PN),
                  data=data1,ntunes=1,totaldraws=200000)

# MCMC
out.nocov=ei.MD.bayes(cbind(GFPV,GUCR,GFAP,GO,GN)~cbind(PFPV,PUCRA,PUCRB,PUCRC,PFAP,PO,PN),
                      covariate=NULL,data=data1,tune.list=tune.nocov,verbose=0,ret.beta="r",thin=80,burnin=100000,ret.mcmc=TRUE) 





# Obtain transition matrix for each mesa: 
source(file.path(getwd(),"Codes","getbetas.R"))
beta11=getbetas(out.nocov,"PFPV","GFPV")
beta12=getbetas(out.nocov,"PFPV","GUCR")
beta13=getbetas(out.nocov,"PFPV","GFAP")
beta14=getbetas(out.nocov,"PFPV","GO")
beta15=getbetas(out.nocov,"PFPV","GN")
beta21=getbetas(out.nocov,"PUCRA","GFPV")
beta22=getbetas(out.nocov,"PUCRA","GUCR")
beta23=getbetas(out.nocov,"PUCRA","GFAP")
beta24=getbetas(out.nocov,"PUCRA","GO")
beta25=getbetas(out.nocov,"PUCRA","GN")
beta31=getbetas(out.nocov,"PUCRB","GFPV")
beta32=getbetas(out.nocov,"PUCRB","GUCR")
beta33=getbetas(out.nocov,"PUCRB","GFAP")
beta34=getbetas(out.nocov,"PUCRB","GO")
beta35=getbetas(out.nocov,"PUCRB","GN")
beta41=getbetas(out.nocov,"PUCRC","GFPV")
beta42=getbetas(out.nocov,"PUCRC","GUCR")
beta43=getbetas(out.nocov,"PUCRC","GFAP")
beta44=getbetas(out.nocov,"PUCRC","GO")
beta45=getbetas(out.nocov,"PUCRC","GN")
beta51=getbetas(out.nocov,"PFAP","GFPV")
beta52=getbetas(out.nocov,"PFAP","GUCR")
beta53=getbetas(out.nocov,"PFAP","GFAP")
beta54=getbetas(out.nocov,"PFAP","GO")
beta55=getbetas(out.nocov,"PFAP","GN")
beta61=getbetas(out.nocov,"PO","GFPV")
beta62=getbetas(out.nocov,"PO","GUCR")
beta63=getbetas(out.nocov,"PO","GFAP")
beta64=getbetas(out.nocov,"PO","GO")
beta65=getbetas(out.nocov,"PO","GN")
beta71=getbetas(out.nocov,"PN","GFPV")
beta72=getbetas(out.nocov,"PN","GUCR")
beta73=getbetas(out.nocov,"PN","GFAP")
beta74=getbetas(out.nocov,"PN","GO")
beta75=getbetas(out.nocov,"PN","GN")
betaout=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   beta61,beta62,beta63,beta64,beta65,
                   beta71,beta72,beta73,beta74,beta75,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)
write.table(betaout,file=file.path(getwd(),"Output","RN_Mesa.txt"),col.names=NA)


# Obtain aggregate transition matrix. 
source(file.path(getwd(),"Codes","getbetasagr.R"))
beta11=getbetasagr(out.nocov,"PFPV","GFPV")
beta12=getbetasagr(out.nocov,"PFPV","GUCR")
beta13=getbetasagr(out.nocov,"PFPV","GFAP")
beta14=getbetasagr(out.nocov,"PFPV","GO")
beta15=getbetasagr(out.nocov,"PFPV","GN")
beta21=getbetasagr(out.nocov,"PUCRA","GFPV")
beta22=getbetasagr(out.nocov,"PUCRA","GUCR")
beta23=getbetasagr(out.nocov,"PUCRA","GFAP")
beta24=getbetasagr(out.nocov,"PUCRA","GO")
beta25=getbetasagr(out.nocov,"PUCRA","GN")
beta31=getbetasagr(out.nocov,"PUCRB","GFPV")
beta32=getbetasagr(out.nocov,"PUCRB","GUCR")
beta33=getbetasagr(out.nocov,"PUCRB","GFAP")
beta34=getbetasagr(out.nocov,"PUCRB","GO")
beta35=getbetasagr(out.nocov,"PUCRB","GN")
beta41=getbetasagr(out.nocov,"PUCRC","GFPV")
beta42=getbetasagr(out.nocov,"PUCRC","GUCR")
beta43=getbetasagr(out.nocov,"PUCRC","GFAP")
beta44=getbetasagr(out.nocov,"PUCRC","GO")
beta45=getbetasagr(out.nocov,"PUCRC","GN")
beta51=getbetasagr(out.nocov,"PFAP","GFPV")
beta52=getbetasagr(out.nocov,"PFAP","GUCR")
beta53=getbetasagr(out.nocov,"PFAP","GFAP")
beta54=getbetasagr(out.nocov,"PFAP","GO")
beta55=getbetasagr(out.nocov,"PFAP","GN")
beta61=getbetasagr(out.nocov,"PO","GFPV")
beta62=getbetasagr(out.nocov,"PO","GUCR")
beta63=getbetasagr(out.nocov,"PO","GFAP")
beta64=getbetasagr(out.nocov,"PO","GO")
beta65=getbetasagr(out.nocov,"PO","GN")
beta71=getbetasagr(out.nocov,"PN","GFPV")
beta72=getbetasagr(out.nocov,"PN","GUCR")
beta73=getbetasagr(out.nocov,"PN","GFAP")
beta74=getbetasagr(out.nocov,"PN","GO")
beta75=getbetasagr(out.nocov,"PN","GN")
betaoutagr=data.frame(beta11,beta12,beta13,beta14,beta15,
                   beta21,beta22,beta23,beta24,beta25,
                   beta31,beta32,beta33,beta34,beta35,
                   beta41,beta42,beta43,beta44,beta45,
                   beta51,beta52,beta53,beta54,beta55,
                   beta61,beta62,beta63,beta64,beta65,
                   beta71,beta72,beta73,beta74,beta75,
                   data$votantes,data$prov,data$comuna,data$circ,data$mesa)

betaoutagr=as.vector(betaoutagr[1,1:140])
betaoutagr=matrix(betaoutagr,4)
betaoutagr_coefs=t(matrix(betaoutagr[1,],5))
betaoutagr_sd=t(matrix(betaoutagr[2,],5))
write.table(betaoutagr_coefs,file=file.path(getwd(),"Output","RNAggregate_Coefs.txt"),col.names=NA)
write.table(betaoutagr_sd,file=file.path(getwd(),"Output","RNAggregate_SD.txt"),col.names=NA)




# save(out.nocov, file = "RNMCMC.Rdata") #Save MCMC runs


# Geweke Convergence Statistics. 
geweke=geweke.diag(out.nocov$draws$Beta)
gewekestats=geweke[[1]]
gewekedf=data.frame(gewekestats)
#write.table(gewekedf,file="RNgewekelist.txt",col.names=NA)

jpeg(file.path(getwd(),"Output","RNHistogramGeweke.jpeg"))
hist(gewekestats)
dev.off()

print("Max Geweke")
print(max(gewekestats))
print("Min Geweke")
print(min(gewekestats))
print("Percentage Above 2.35")
print(length(gewekestats[abs(gewekestats)>2.35])/length(gewekestats))
print("Percentage Above 1.96")
print(length(gewekestats[abs(gewekestats)>1.96])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1.5])/length(gewekestats))
print("Percentage Above 1.5")
print(length(gewekestats[abs(gewekestats)>1])/length(gewekestats))
print("Percentage Above 0.5")
print(length(gewekestats[abs(gewekestats)>0.5])/length(gewekestats))
